package com.dao;

import com.model.Build;
import com.model.PageBean;
import com.model.Repair;
import com.model.User;
import com.util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangjg
 * @date 2023/11/1 15:15
 * @Description: 维修申请处理DAO
 */
public class RepairDao {

    public List<Repair> repairList(Connection con, PageBean pageBean, Repair repair)throws Exception {
        List<Repair> repairList = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select * from t_repair t1");
        if(StringUtil.isNotEmpty(repair.getItem())) {
            sb.append(" where t1.item like '%").append(repair.getItem()).append("%'");
        }
        if(pageBean != null) {
            sb.append(" limit ").append(pageBean.getStart()).append(",").append(pageBean.getPageSize());
        }
        PreparedStatement preparedStatement = con.prepareStatement(sb.toString());
        ResultSet rs = preparedStatement.executeQuery();
        while(rs.next()) {
            Repair repair1 = new Repair();
            repair1.setId(rs.getInt("id"));
            repair1.setItem(rs.getString("item"));
            repair1.setDescription(rs.getString("description"));
            repair1.setDate(rs.getString("date"));
            repair1.setUserId(rs.getInt("userId"));
            repair1.setUserName(rs.getString("userName"));
            repair1.setBuildId(rs.getInt("buildId"));
            repair1.setBuildName(rs.getString("buildName"));
            repair1.setRoomId(rs.getInt("roomId"));
            repair1.setRoomNum(rs.getString("roomNum"));
            repair1.setStatus(rs.getString("status"));
            repair1.setRepairor(rs.getString("repairor"));
            repairList.add(repair1);
        }
        return repairList;
    }

    public Repair repairInfo(Connection con, int id)throws Exception {
        String sql = "select * from t_repair where id=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        ResultSet rs = preparedStatement.executeQuery();
        if(rs.next()) {
            Repair repair = new Repair();
            repair.setId(rs.getInt("id"));
            repair.setItem(rs.getString("item"));
            repair.setDescription(rs.getString("description"));
            repair.setDate(rs.getString("date"));
            repair.setUserId(rs.getInt("userId"));
            repair.setUserName(rs.getString("userName"));
            repair.setBuildId(rs.getInt("buildId"));
            repair.setBuildName(rs.getString("buildName"));
            repair.setRoomId(rs.getInt("roomId"));
            repair.setRoomNum(rs.getString("roomNum"));
            repair.setStatus(rs.getString("status"));
            repair.setRepairor(rs.getString("repairor"));
            return repair;
        }
        return null;
    }

    public int repairCount(Connection con, Repair repair)throws Exception {
        StringBuilder sb = new StringBuilder("select count(*) as total from t_repair t1");
        if(StringUtil.isNotEmpty(repair.getItem())) {
            sb.append(" where t1.item like '%").append(repair.getItem()).append("%'");
        }
        PreparedStatement preparedStatement = con.prepareStatement(sb.toString());
        ResultSet rs = preparedStatement.executeQuery();
        if(rs.next()) {
            return rs.getInt("total");
        } else {
            return 0;
        }
    }


    public int addRepair(Connection con, Repair repair)throws Exception {
        String sql = "insert into t_repair values(null,?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, repair.getItem());
        preparedStatement.setString(2, repair.getDescription());
        preparedStatement.setString(3, repair.getDate());
        preparedStatement.setInt(4, repair.getUserId());
        preparedStatement.setString(5, repair.getUserName());
        preparedStatement.setInt(6, repair.getBuildId());
        preparedStatement.setString(7, repair.getBuildName());
        preparedStatement.setInt(8, repair.getRoomId());
        preparedStatement.setString(9, repair.getRoomNum());
        preparedStatement.setString(10, repair.getStatus());
        preparedStatement.setString(11, repair.getRepairor());
        return preparedStatement.executeUpdate();
    }

    public int deleteRepair(Connection con, String id)throws Exception {
        String sql = "delete from t_repair where id=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, id);
        return preparedStatement .executeUpdate();
    }

    public int updateRepair(Connection con, Repair repair)throws Exception {
        String sql = "update t_repair set item=?,description=? ,date=?,userId=?,userName=?,buildId=?,buildName=?,roomId=?,roomNum=?,status=?,repairor=? where id=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, repair.getItem());
        preparedStatement.setString(2, repair.getDescription());
        preparedStatement.setString(3, repair.getDate());
        preparedStatement.setInt(4, repair.getUserId());
        preparedStatement.setString(5,repair.getUserName());
        preparedStatement.setInt(6, repair.getBuildId());
        preparedStatement.setString(7, repair.getBuildName());
        preparedStatement.setInt(8, repair.getRoomId());
        preparedStatement.setString(9, repair.getRoomNum());
        preparedStatement.setString(10, repair.getStatus());
        preparedStatement.setString(11, repair.getRepairor());
        preparedStatement.setInt(12, repair.getId());
        return preparedStatement.executeUpdate();
    }
}
